#include "turnovermanagement.h"
#include "ui_turnovermanagement.h"

turnoverManagement::turnoverManagement(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::turnoverManagement)
{
    ui->setupUi(this);
    ui->confirmBtn->setShortcut(Qt::Key_Return);//设置保存快捷键为键盘的“回车”键
}

turnoverManagement::~turnoverManagement()
{
    delete ui;
}

void turnoverManagement::on_confirmBtn_clicked()
{
    ui->ordersList->clear();//清空表格
    QSqlQuery query;//创建对象时，系统会自动完成跟数据库的关联
    query.setForwardOnly(true);

    QString keyDate;
    double total = 0;

    if(ui->yearRb->isChecked()){
        keyDate = ui->dateEdit->date().toString("yyyy/MM/dd").remove(4,6);
    }else if(ui->monthRb->isChecked()){
        keyDate = ui->dateEdit->date().toString("yyyy/MM/dd").remove(7,3);
    }else if(ui->dayRb->isChecked()){
        keyDate = ui->dateEdit->date().toString("yyyy/MM/dd");
    }else{
        QMessageBox::information(this,"友情提示","请选择查看方式");
        return;
    }
    //确定查找关键词

    QString sqlSelect = QString ("select * from orders where state = '%1';").arg("已完成");
    if(!query.exec(sqlSelect)){//遍历表单
        qDebug() << "select data error" << db.lastError();
    }else {
        while(query.next()){//读取数据库，填写列表
            int id = query.value("id").toInt();
            QString date = query.value("date").toString();
            if(date.startsWith(keyDate)){
                QString address = query.value("address").toString();
                QString customer = query.value("customer").toString();
                QString phone = query.value("phone").toString();
                QString state = query.value("state").toString();
                QString content = query.value("content").toString();
                total += query.value("prize").toDouble();
                ordersForm* ordersItem = new ordersForm;
                ordersItem->setOrders(id,date,address,customer,phone,state);
                QListWidgetItem* item = new QListWidgetItem;

                item->setSizeHint(QSize(1420,70));
                ui->ordersList->addItem(item);
                ui->ordersList->setItemWidget(item,ordersItem);
            }
        }
    }
    ui->totalLb->setText(QString::number(total) + "元");
}
